package com.neuedu.shixun.hospital2.service.Impl;
import com.neuedu.shixun.hospital2.entity.Doctor;
import com.neuedu.shixun.hospital2.entity.PingLun;
import com.neuedu.shixun.hospital2.entity.PingLun2;
import com.neuedu.shixun.hospital2.entity.User;
import com.neuedu.shixun.hospital2.repository.DoctorRepository;
import com.neuedu.shixun.hospital2.repository.PingLunRepository;
import com.neuedu.shixun.hospital2.repository.UserRepository;
import com.neuedu.shixun.hospital2.service.PingLunService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;

import java.util.List;
import java.util.Optional;
@Service
@Slf4j
public class PingLunServiceImpl implements PingLunService {
    @Autowired
    private PingLunRepository pingLunRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private DoctorRepository doctorRepository;

    @Override
    public List<PingLun> getAll() {
        return pingLunRepository.findAll();
    }

    @Override
    public List<PingLun2> getAll2(List<PingLun> list) {
        List<PingLun2> list1=new ArrayList<>();
        for (PingLun pingLun:list){
            PingLun2  pingLun2=new PingLun2();
            pingLun2.setPid(pingLun.getPid());
            pingLun2.setPdate(pingLun.getPdate());
            pingLun2.setPxingji(pingLun.getPxingji());
            pingLun2.setPcontent(pingLun.getPcontent());
            pingLun2.setUUid(pingLun.getUid());
            Optional<User> optionalUser = userRepository.findById(pingLun.getUid());
            User user=new User();
            if(optionalUser.isPresent()){
               user=optionalUser.get();
            }
            pingLun2.setUidcard(user.getUidcard());
            pingLun2.setUName(user.getUname());
            pingLun2.setUaddress(user.getUaddress());
            pingLun2.setUhead(user.getUhead());
            pingLun2.setUNicheng(user.getUNicheng());
            pingLun2.setDDid(pingLun.getDid());
            Doctor doctor=new Doctor();

            Optional<Doctor> optionalDoctor = doctorRepository.findById(pingLun.getDid());
            if (optionalDoctor.isPresent())
               doctor=optionalDoctor.get();
            pingLun2.setDDname(doctor.getDDname());
            pingLun2.setDTitle(doctor.getDTitle());
            pingLun2.setPStatus(pingLun.getPStatus());
            list1.add(pingLun2);
        }
        return list1;
    }

    @Override
    public PingLun update(Integer pid, Boolean status) {
      PingLun pingLun =pingLunRepository.getOne(pid);
      pingLun.setPStatus(status);
      return pingLunRepository.save(pingLun);
    }

    @Override
    public Page<PingLun2> findByPage(int pageCode, int pageSize) {
        Pageable pageable = new PageRequest(pageCode, pageSize);
        Page<PingLun> pingLuns=pingLunRepository.findAll(pageable);
//        log.info("jibingaaaaa"+pingLuns);
        List<PingLun2> list=getAll2(pingLuns.getContent());
        log.info("pinlun"+list);
        return new PageImpl<>(list);
    }
}
